package com.zhenhao.xsq.utils;

import android.util.Log;

import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.db.green.DaoSession;
import com.zhenhao.xsq.db.green.UserLoginInfo;
import com.zhenhao.xsq.db.green.UserLoginInfoDao;

import java.util.List;

/**
 * 数据库工具类
 * Created by William on 2019/5/15.
 */
public class DbUtils {
    private final static String TAG = "DB";
    private DaoSession mDaoSession;

    private UserLoginInfoDao mUserLoginInfoDao;

    private DbUtils(DaoSession daoSession) {
        this.mDaoSession = daoSession;
        mUserLoginInfoDao = this.mDaoSession.getUserLoginInfoDao();
    }

    private static DbUtils mDbUtils;

    public static DbUtils getInstans() {
        if (mDbUtils == null) {
            mDbUtils = new DbUtils(AppConfig.getGreenDaoSession());
        }
        return mDbUtils;
    }

    /**
     * 增加一条登录用户信息
     */
    public void insertOneUserLoginInfo(UserLoginInfo userLoginInfo) {
        mUserLoginInfoDao.insert(userLoginInfo);
    }

    /**
     * 查询所有登录用户信息
     */
    public List<UserLoginInfo> quaryAllUserLoginInfo() {
        List<UserLoginInfo> list = mUserLoginInfoDao.queryBuilder().list();
        if (list != null) {
            Log.e(TAG, "查询所有登录用户信息成功" + list.toString());
            return list;
        } else {
            return null;
        }
    }

    /**
     * 更新一条登录用户信息
     *
     * @param userLoginInfo
     */
    public void updateOneUserLoginInfo(UserLoginInfo userLoginInfo) {
        mUserLoginInfoDao.update(userLoginInfo);
    }

    /**
     * 删除一条登录用户信息
     * @param userLoginInfo
     */
    public void deleteUserLoginInfo(UserLoginInfo userLoginInfo){
        mUserLoginInfoDao.delete(userLoginInfo);
    }
}
